package com.bestcem.xm.txtanls.mq.receiver.biz;

import cn.hutool.json.JSONUtil;
import com.bestcem.xm.txtanls.grpc.service.param.analysis.LinkageUpdateParam;
import com.bestcem.xm.txtanls.service.AnalysisProjectService;
import com.bestcem.xm.txtanls.service.dto.mq.WorktableChanged;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ReceiverService {

    @Resource
    private AnalysisProjectService analysisProjectService;

    /**
     * 更新所有使用对应数据源的文本分析项目的数据表名称
     */
    public void worktableChanged(GenericMessage<WorktableChanged> msg) {
        WorktableChanged dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("【文本分析】 更新所有使用对应数据源的文本分析项目的数据表名称:{}", JSONUtil.toJsonStr(dto));
        }
        analysisProjectService.linkageUpdate(LinkageUpdateParam.getInstance(dto));
    }

    /**
     * 数据源更新字段时联动更新文本分析中的项目
     */
    public void columnChanged(GenericMessage<WorktableChanged> msg) {
        WorktableChanged dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("【文本分析】 数据源更新字段时联动更新文本分析中的项目:{}", JSONUtil.toJsonStr(dto));
        }
        analysisProjectService.linkageUpdate(LinkageUpdateParam.getInstance(dto));
    }
}
